<?php

namespace App\View\Components;

use App\Models\Warranty;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\View\Component;

class WarrantyImage extends Component
{
    /**
     * Create a new component instance.
     *
     * @return void
     */
    public function __construct(Request $request, Warranty $warranty)
    {
        $this->warranty_id = $warranty->id;
        $this->images = [];
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\View\View|string
     */
    public function render()
    {
        $images = $this->getImages();

        return view('components.warranty-image', [
            'images' => $images,
        ]);
        return view('components.warranty-image');
    }

    public function getImages()
    {
        if (empty($this->warranty_id)) {
            return [];
        } else {
            $images = DB::table('warranty_image')
                ->where('warranty_id', $this->warranty_id)
                ->whereNull('deleted_at')
                ->get(['id', 'image_src', 'part_id', 'warranty_id']);
            return $images;
        }

    }
}
